package hsrt.vis;

import java.lang.reflect.Array;
import java.util.HashMap;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.opengles.GL10;

import android.R.array;
import android.app.Activity;
import android.util.Log;


public class SphereRenderer implements SphereView.Renderer {
	
	private Sphere mSphere;
    private float mAngleY;
    private float posX;
    private float posY;
	private boolean first=false;
	private int selArea=1;
	private String selectedDataText;
	
	public SphereRenderer(Activity activity) {
       mSphere = new StatusDataSphere(activity);
       mSphere.Create();
    }
		
	/* This Function sets the rotation angle of the sphere */	
   public void rotateY(float f){
	   	if (this.mAngleY + f < 0 ) {
	   		this.mAngleY=356;
	   	} else if (this.mAngleY + f>360) {
	   		this.mAngleY=4;
	   	} else {
	   		this.mAngleY += f;
	   	}
        setPart(this.mAngleY);
   	} 
	
   /* Returns Rotation angle */
	public Float getAngleY() {
		return this.mAngleY;
	}
	
	/* set local variable for current visible Area on sphere (1-max)*/
	public void setArea(int val) {
		this.selArea=val;
	}
	
	/* This function sets current visible Area on Sphere an map the title of each area */
	@SuppressWarnings("null")
	public void setPart(float angle) {
		int partCount=mSphere.getDataSize();
		int partSize=360/partCount;

		String[] dataMapping = {"null","BatteryStrenth","FileSystem","SignalStrength","CallStatus"};
		
		if (angle>0 && angle<partSize*1) {
			setArea(1);
		} else if (angle>350 && getSelArea()==1){
			setArea(partCount);
		} else if (angle>partSize*getSelArea() && getSelArea()<4) {
			setArea(getSelArea()+1);
		} else if (angle<partSize*(getSelArea()-1)) {
			setArea(getSelArea()-1);
		}
		
		for (int i=1;i<=partCount;i++) {
			if (getSelArea()==i) {
				setSelectedDataText(dataMapping[i]);
			}
		}
					
	}
	
	/* This Function updates the local variable of textfield value */
	public void setSelectedDataText (String text) {
		this.selectedDataText=text;
	}
	
	/* Get current value of local textfield value */
	public String getSelectedDataText() {
		return this.selectedDataText;
	}
	/* Get current visible part (1-max) */
	public int getSelArea() {
		return this.selArea;
	}
	

/**************************************************************************************/
	
	public void drawFrame(GL10 gl) {
        /*
         * Usually, the first thing one might want to do is to clear
         * the screen. The most efficient way of doing this is to use
         * glClear().
         */
		
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
        /*
         * Now we're ready to draw some 3D objects
         */
        
		gl.glMatrixMode(gl.GL_MODELVIEW); 
        gl.glLoadIdentity();
        
        gl.glPushMatrix();
        gl.glTranslatef(0, -0.2f, -3.0f);
        gl.glScalef(1.2f, 1.2f, 1.2f); 
        
        if (mAngleY<0) {
        	this.mAngleY=0;
        }
        gl.glRotatef(mAngleY,0, 1, 0); 
                
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
        
        mSphere.draw(gl);
        gl.glPopMatrix();

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}

	public int[] getConfigSpec() {
        // We want a depth buffer, don't care about the
        // details of the color buffer.
        int[] configSpec = {
        		EGL10.EGL_BUFFER_SIZE,   16,
        		EGL10.EGL_DEPTH_SIZE, 16,
                EGL10.EGL_NONE
        };
        return configSpec;
	}
	
	public void sizeChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);

        /*
         * Set our projection matrix. This doesn't have to be done
         * each time we draw, but usually a new projection needs to
         * be set when the viewport is resized.
         */

        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
   }

   public void surfaceCreated(GL10 gl) {
       /*
        * By default, OpenGL enables features that improve quality
        * but reduce performance. One might want to tweak that
        * especially on software renderer.
        */
       gl.glDisable(GL10.GL_DITHER);

       /*
        * Some one-time OpenGL initialization can be made here
        * probably based on features of this particular context
        */
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
                GL10.GL_FASTEST);

        gl.glClearColor(0,0,0,0);
        
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glEnable(GL10.GL_DEPTH_TEST);

        
   }
}
